package org.niit.dao

import org.niit.util.JDBCUtil

import java.sql.Connection

/**
 * Date:2025/6/4
 * Author：Ys
 * Description:  主要是用来操作数据库
 */
class UserAdDao {

  def selectBlackUserById(userid:String):Boolean={
    //查询数据库中黑名单表，判断用户是否在黑名单中

    //1.获得MySQL连接
    val conn: Connection = JDBCUtil.getConnection
    //2.编写SQL
    val bool = JDBCUtil.isExist(conn,"select * from black_list where userid = ?",Array(userid))
    //3.关闭连接
    conn.close()
    bool
  }

  def insertBlackUser(userid:String):Unit={
    //将用户拉入黑名单
    //1.获得MySQL连接
    val conn: Connection = JDBCUtil.getConnection
    //2.编写SQL
    JDBCUtil.executeUpdate(conn,
      """insert into black_list(userid)
        |values(?)
        |on duplicate key update userid = ?
        |""".stripMargin,
      Array(userid,userid))
    //3.关闭连接
    conn.close()
  }

  def insertUserAdCount(day:String,userid:String,adid:String,count:Int):Unit={
    val conn = JDBCUtil.getConnection

    JDBCUtil.executeUpdate(conn,
      """
        |insert into user_ad_count(dt,userid,adid,count)
        | values (?,?,?,?) on duplicate key update
        |  count = count +?
        |""".stripMargin,
      Array(day,userid,adid,count,count))

    conn.close()

  }

  //查看用户的点击广告次数，是否超过规定次数
  def checkUserAdCount(day:String,userid:String,adid:String,count:Int):Boolean={
    val conn = JDBCUtil.getConnection

    val bool = JDBCUtil.isExist(conn,
      """
        |select * from user_ad_count
        | where dt = ? and userid = ? and adid = ? and count >= ?
        |""".stripMargin,Array(day,userid,adid,count))

     conn.close()
    bool

  }

}


